package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.eventbus_event.AuthScanEvent;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.QRCodeUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthProcessUkidBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthorityDistributeItemBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.BatchAuthBean;
import com.wwwarehouse.usercenter.bean.authoritydistribute.ScanCodeMsgBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.fragment.invite.InviteRegisterFragment;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;


/**
 * @author : mundane
 * @time : 2017/9/22 11:54
 * @description :
 * @file : AuthorityDistributeQRcodeFragment.java
 */

public class AuthorityDistributeQRcodeFragment extends BaseFragment {

	private ImageView mIvQRcode;
	private View mLlContact;
	private View mLlInviteDistribute;
	private AuthProcessUkidBean mAuthProcessUkid;
	private String mBusinessUnitId;
	private ArrayList<AuthorityDistributeItemBean> mSelectedUserBeanList;
	private View mFlContact;
	private View mFlInviteDistribute;
	private View mBtnDistributeOther;
	private View mBtnBack;
	private TextView mTvPeopleNum;
	private TextView mTvFailDetail;
	private String mProcessId;

	public static AuthorityDistributeQRcodeFragment newInstance(AuthProcessUkidBean authProcessUkidBean, String businessUnitId, ArrayList<AuthorityDistributeItemBean> selectedDataList) {
		AuthorityDistributeQRcodeFragment fragment = new AuthorityDistributeQRcodeFragment();
		Bundle bundle = new Bundle();
		bundle.putParcelable(UserCenterConstant.KEY_AUTH_PROCESS_UKID, authProcessUkidBean);
		bundle.putString(UserCenterConstant.KEY_BUSINESS_UNIT_ID, businessUnitId);
		bundle.putParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_ITEM_BEAN, selectedDataList);
		fragment.setArguments(bundle);
		return fragment;
	}

	private int mPeopleNumber;

	public void onEventMainThread(AuthScanEvent event) {
		LogUtils.showInfoLog("event.getMsg() = " + event.getMsg());
		ScanCodeMsgBean scanCodeMsg = JSON.parseObject(event.getMsg(), ScanCodeMsgBean.class);
		if (scanCodeMsg.msg.hasFail == true) {
			if (mTvFailDetail.getVisibility() != View.VISIBLE) {
				mTvFailDetail.setVisibility(View.VISIBLE);
			}
		}
		if (TextUtils.equals(String.valueOf(mProcessId), scanCodeMsg.msg.processId)) {
			mPeopleNumber++;
		}
		mTvPeopleNum.setText(String.format("已有%d人通过扫码获得权限", mPeopleNumber));
		if (mPeopleNumber > 0) {
			mBtnDistributeOther.setVisibility(View.VISIBLE);
			mBtnBack.setVisibility(View.VISIBLE);
			mBtnBack.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
				}
			});
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}
		Bundle bundle = getArguments();
		if (bundle != null) {
			mAuthProcessUkid = bundle.getParcelable(UserCenterConstant.KEY_AUTH_PROCESS_UKID);
			mProcessId = mAuthProcessUkid.processId;
			mBusinessUnitId = bundle.getString(UserCenterConstant.KEY_BUSINESS_UNIT_ID);
			mSelectedUserBeanList = bundle.getParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_ITEM_BEAN);
		}
	}

	@Override
	public void onDestroy() {
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_qrcode_distribute_authority, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		// findViewById
		mIvQRcode = $(R.id.iv_qr_code);
		mLlContact = $(R.id.ll_contact);
		mLlInviteDistribute = $(R.id.ll_invite_distribute);
		mFlContact = $(R.id.fl_contact);
		mFlInviteDistribute = $(R.id.fl_invite_distribute);
		mBtnDistributeOther = $(R.id.btn_distribute_other);
		mBtnDistributeOther.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				popFragment();
			}
		});
		mBtnBack = $(R.id.btn_back);
		mTvPeopleNum = $(R.id.tv_people_num);
		mTvFailDetail = $(R.id.tv_fail_detail);
		mTvFailDetail.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				pushFragment(AuthorityDistributeAuserDetailPageFragment.newInstance(mProcessId, 0));
			}
		});
		if (mAuthProcessUkid != null) {
			mFlContact.setVisibility(mAuthProcessUkid.show ? View.VISIBLE : View.GONE);
		}
		mLlContact.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                if (mAuthProcessUkid != null) {
                    SelectDistributionPermissionPagerFragment fragment = new SelectDistributionPermissionPagerFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(UserCenterConstant.KEY_BATCH_AUTH, new BatchAuthBean(mBusinessUnitId, mAuthProcessUkid, mSelectedUserBeanList));
                    fragment.setArguments(bundle);
                    pushFragment(fragment);
                }

            }
        });

        mLlInviteDistribute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(UserCenterConstant.KEY_AUTH_PROCESS_UKID, mProcessId);
				InviteRegisterFragment fragment = new InviteRegisterFragment();
				fragment.setArguments(bundle);
				pushFragment(fragment);
			}
        });

        if (mAuthProcessUkid != null) {
            Bitmap bitmap = QRCodeUtils.createQRCode(mAuthProcessUkid.qrCode);
            mIvQRcode.setImageBitmap(bitmap);
        }

    }

    @Override
    public void setTitle() {
		if (peekFragment() instanceof AuthorityDistributeQRcodeFragment) {
			mActivity.setTitle("权限分配");
		}
    }

    @Override
    public void requestDatas() {

    }
}
